package com.fractalist.base.core.helper;

import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import com.fractalist.base.core.model.SearchDate;

/**
 * 项目的帮助类，一些常用的琐碎的静态方法
 * 
 * @author radio
 * 
 */
public class ApplicationHelper {

	/**
	 * 根据map参数来组装查询sql语句，如果没有查询条件返回"where 1=1"<br>
	 * map的key为要查询的表的数据库字段或对象属性，map的value为要查询的值<br>
	 * 字符串类型使用like进行模糊查询，searchDate类型使用组装的时间进行查询<br>
	 * 
	 * @see SearchDate#toSql(String)
	 * 
	 * @param map
	 *            搜索条件
	 * @return 组装好的sql语句
	 */
	public static String buildSqlFromMap(Map<String, Object> map) {
		StringBuffer sb = new StringBuffer(" where 1=1");
		if (map != null) {
			Iterator<String> it = map.keySet().iterator();
			while (it.hasNext()) {
				String key = it.next();
				Object obj = map.get(key);
				if (null != obj) {
					if (obj instanceof String) {
						String value = (String) obj;
						if (!"".equalsIgnoreCase(value)) {
							sb.append(" and ").append(key).append(" like '%")
									.append(value).append("%'");
						}
					}
					if (obj instanceof Integer) {
						Integer value = (Integer) obj;
						sb.append(" and ").append(key).append(" = ").append(
								value.toString());
					}
					if (obj instanceof Long) {
						Long value = (Long) obj;
						sb.append(" and ").append(key).append(" = ").append(
								value.toString());
					}
					if (obj instanceof SearchDate) {
						SearchDate searchDate = (SearchDate) obj;
						sb.append(searchDate.toSql(key));
					}
				}
			}
		}
		return sb.toString();
	}

	/**
	 * 根据map参数来组装查询sql语句，如果没有查询条件返回"where 1=1"<br>
	 * map的key为要查询的表的数据库字段或对象属性，map的value为要查询的值<br>
	 * 字符串类型使用like进行模糊查询，searchDate类型使用组装的时间进行查询<br>
	 * 
	 * @see SearchDate#toSql(String)
	 * 
	 * @param map
	 *            搜索条件
	 * @return 组装好的sql语句
	 */
	public static String buildSqlFromMapDateNotToNow(Map<String, Object> map) {
		StringBuffer sb = new StringBuffer(" where 1=1");
		if (map != null) {
			Iterator<String> it = map.keySet().iterator();
			while (it.hasNext()) {
				String key = it.next();
				Object obj = map.get(key);
				if (null != obj) {
					if (obj instanceof String) {
						String value = (String) obj;
						if (!"".equalsIgnoreCase(value)) {
							sb.append(" and ").append(key).append(" like '%")
									.append(value).append("%'");
						}
					}
					if (obj instanceof Integer) {
						Integer value = (Integer) obj;
						sb.append(" and ").append(key).append(" = ").append(
								value.toString());
					}
					if (obj instanceof Long) {
						Long value = (Long) obj;
						sb.append(" and ").append(key).append(" = ").append(
								value.toString());
					}
					if (obj instanceof SearchDate) {
						SearchDate searchDate = (SearchDate) obj;
						sb.append(searchDate.toSqlNotToNow(key));
					}
				}
			}
		}
		return sb.toString();
	}

	/**
	 * 检查对象是否为空，字符串判断是否为“”，整数判断是否为0，List和Map判断是否有元素
	 * 
	 * @param obj
	 *            被检查的对象，支持字符串类型，整数类型，List类型，Map类型
	 * @return
	 */
	@SuppressWarnings("unchecked")
	public static boolean isBlank(Object obj) {
		if (obj instanceof String) {
			String temp = (String) obj;
			return temp == null || temp.equalsIgnoreCase("");
		}
		if (obj instanceof Integer) {
			Integer temp = (Integer) obj;
			return temp == null || temp == 0;
		}
		if (obj instanceof Long) {
			Long temp = (Long) obj;
			return temp == null || temp == Long.valueOf(0);
		}
		if (obj instanceof List) {
			List<?> temp = (List<?>) obj;
			return temp == null || temp.size() == 0;
		}
		if (obj instanceof Map) {
			Map<String, Object> temp = (Map<String, Object>) obj;
			return temp == null || temp.isEmpty();
		}
		return obj == null;
	}

	/**
	 * 根据搜索条件参数来组装分页链接
	 * 
	 * @see SearchDate#toLink()
	 * 
	 * @param links
	 *            链接中的搜索条件参数
	 * @return 组装好的分页链接
	 */
	public static String buildSearchLink(Map<String, Object> links) {
		StringBuffer sb = new StringBuffer("");
		if (links != null) {
			Iterator<String> it = links.keySet().iterator();
			while (it.hasNext()) {
				String key = it.next();
				Object obj = links.get(key);
				if (null != obj) {
					if (obj instanceof String) {
						String value = (String) obj;
						try {
							value = URLEncoder.encode(value, "UTF-8");
						} catch (UnsupportedEncodingException e) {
							value = "";
						}
						if (!"".equalsIgnoreCase(value)) {
							sb.append(key).append("=").append(value)
									.append("&");
						}
					}
					if (obj instanceof Integer) {
						Integer value = (Integer) obj;
						sb.append(key).append("=").append(value.toString())
								.append("&");
					}
					if (obj instanceof SearchDate) {
						SearchDate searchDate = (SearchDate) obj;
						sb.append(searchDate.toLink());
					}
				}
			}
		}
		return sb.toString();
	}
}
